package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.InStockInfoMapper;
import com.goods.business.mapper.InStockMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.mapper.SupplierMapper;
import com.goods.business.service.InStockService;
import com.goods.business.service.SupplierService;
import com.goods.common.model.business.InStock;
import com.goods.common.model.business.InStockInfo;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.Supplier;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class InStockServiceImpl implements InStockService {

    @Autowired
    private InStockMapper inStockMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private InStockInfoMapper inStockInfoMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private SupplierService supplierService;

    @Override
    public PageVO<InStockVO> findInStockList(Integer pageNum, Integer pageSize,
                                           InStockVO inStockVO) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(InStock.class);

        Integer status = inStockVO.getStatus();         //物资状态

        Example.Criteria criteria = o.createCriteria();
        if(null!=status&&!"".equals(status)){
            criteria.andLike("status","%"+status+"%");
        }

        List<InStock> inStockList = inStockMapper.selectByExample(o);
        List<InStockVO> inStockVOList = new ArrayList<>();
        for (InStock inStock : inStockList) {
            InStockVO inStockVO1 = new InStockVO();
            BeanUtils.copyProperties(inStock,inStockVO1);
            //获取物资来源对象 和联系方式等
            Long supplierId = inStock.getSupplierId();
            Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
            String phone = supplier.getPhone();
            String name = supplier.getName();

            inStockVO1.setPhone(phone);
            inStockVO1.setSupplierName(name);
            inStockVOList.add(inStockVO1);
        }
        PageInfo<InStockVO> info=new PageInfo<>(inStockVOList);
        return new PageVO<>(info.getTotal(),inStockVOList);
    }

    //入库详情查询
    @Override
    public InStockDetailVO detail(Long id) {
        //获取stock对象
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        //获取supplierVO对象
        Long supplierId = inStock.getSupplierId();
        Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);

        //获取stockItemVo对象，即数据库中的stockInfo
        String inNum = inStock.getInNum();

        Example o = new Example(InStockInfo.class);
        Example.Criteria criteria = o.createCriteria();
        if(null!=inNum&&!"".equals(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }
        List<InStockInfo> inStockInfos = inStockInfoMapper.selectByExample(o);
        //stream流 偷天换日-->转换成ItemVos
        List<InStockItemVO> itemVos = inStockInfos.stream().map(inStockInfo -> {
            InStockItemVO inStockItemVO = new InStockItemVO();

            String pNum = inStockInfo.getPNum();
            inStockItemVO.setPNum(pNum);

            Example o1 = new Example(Product.class);
            Example.Criteria criteria1 = o1.createCriteria();
            if(null!=pNum&&!"".equals(pNum)){
                criteria1.andEqualTo("pNum",pNum);
            }
            Product product = productMapper.selectOneByExample(o1);

            inStockItemVO.setName(product.getName());
            inStockItemVO.setModel(product.getModel());
            inStockItemVO.setUnit(product.getUnit());
            inStockItemVO.setImageUrl(product.getImageUrl());
            inStockItemVO.setCount(inStockInfo.getProductNumber());
            return inStockItemVO;
        }).collect(Collectors.toList());

        //创建入库详情对象InStockDetailVO
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        inStockDetailVO.setInNum(inNum);
        inStockDetailVO.setStatus(inStock.getStatus());
        inStockDetailVO.setType(inStock.getType());
        inStockDetailVO.setOperator(inStock.getOperator());
        inStockDetailVO.setSupplierVO(supplierVO);
        inStockDetailVO.setTotal(itemVos.size());
        inStockDetailVO.setItemVOS(itemVos);

        return inStockDetailVO;
    }

    @Override
    public void remove(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock);
    }

    @Override
    public void back(Long id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
    }

    @Override
    public void delete(Long id) {
        inStockMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void addIntoStock(InStockVO inStockVO,Supplier supplier) {
        //判断，是否要新增物资来源
        if (null != supplier) {
            supplierService.add(supplier);
        }
        //设置inStock对象的in_num入库单号
        InStock inStock = new InStock();
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        inStock.setInNum(uuid);
        inStock.setType(inStockVO.getType());
        inStock.setOperator(inStockVO.getOperator());
        inStock.setSupplierId(inStockVO.getSupplierId());
//        inStock.setProductNumber();
//        inStock.setRemark();
//        inStock.setStatus();
        inStock.setType(inStockVO.getType());



        //修改表操作
        //  获取products信息
        List<Object> products = inStockVO.getProducts();
        inStock.setProducts(products);

        List<Map> productMaps = products.stream().map(o -> {
            Map productMap = (Map) o;
            return productMap;
        }).collect(Collectors.toList());

        for (Map productMap : productMaps) {
            InStockInfo inStockInfo = new InStockInfo();
            inStockInfo.setInNum(uuid);
            inStockInfo.setPNum((String) productMap.get("pNum"));
            inStockInfo.setProductNumber((Integer) productMap.get("productNumber"));
            inStockInfoMapper.insert(inStockInfo);
        }



    }
}
